package com.outstanding.ifml;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.outstanding.ifml.exception.IFMLException;


/**
 * 一个对象，代表了一列或一个字段定义
 * 为了后面的差异比较，所以实现了排序接口
 * @author chenzhixuan
 *
 */
public class Item implements Comparable{
	//层级
	private int level = -1;
	
	
	public int getLevel() {
		return level;
	}

	public Item(int level) {
		this.level = level;
	}
	// 字段名字
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		if(value instanceof List) {
			setItemClass(ItemValueClass.array);
		}
		this.value = value;
	}

	// 字段的值
	private Object value;

	// 字段的类型,可以传一个枚举的值
	private String itemClass;

	public String getItemClass() {
		return itemClass;
	}

	public void setItemClass(ItemValueClass itemClass) {
		this.itemClass = itemClass.name();
	}

	public void setItemClass(String itemClass) {
		this.itemClass = itemClass;
	}

	// 属性的集合
	private List<ItemProperty> properties = new ArrayList<ItemProperty>();

	// 子元素，当itemClass为Object 和 listObject 时就有子元素
	private List<Item> childItem = new ArrayList<Item>();

	public void addProperty(ItemProperty property) {
		this.properties.add(property);
	}

	public void addChildIf(Item child) {
		if(ItemValueClass.listobject.name().equals(this.itemClass)) {
			
		}else {
			setItemClass(ItemValueClass.object);
		}
		this.childItem.add(child);
	}
	
	
	public String toString() {
		
		StringBuffer liner = new StringBuffer();
		for(int i =0;i<this.level;i++) {
			liner.append(IFMLPaserProperties.BLANK);
		}
		liner.append(name);
		liner.append("(");
		liner.append("type:");
		liner.append("\"");
		liner.append(this.itemClass);
		liner.append("\"");
		if(this.properties != null && this.properties.size() >0) {
			liner.append(",");
			int j = 0;
			for(ItemProperty ip:this.properties) {
				j++;
				liner.append(ip.toString());
				if(j<this.properties.size()) {
					liner.append(",");
				}
			}
			
		}
		liner.append(")");
		if(this.value != null) {
			liner.append(":");
			if(this.value instanceof List) {
				List<String> list = (List<String>)this.value;
				liner.append("[");
				int k = 0;
				for(String str:list) {
					k++;
					liner.append("\"");
					liner.append(replace(str));
					liner.append("\"");
					if(k <list.size()) {
						liner.append(",");
					}
				}
				liner.append("]");
			}else {
				liner.append("\"");
				liner.append(replace(this.value.toString()));
				liner.append("\"");
			}
			
		}
		liner.append("\n");
		if(this.childItem != null && this.childItem.size() >0) {
			for(Item child:childItem) {
				liner.append(child.toString());
			}
		}
		return liner.toString();
	}

	
    static	public String replace(String val) {
    		String newStr = val.replaceAll("\"", "\\\\\"");
    		newStr = newStr.replaceAll("'", "\\\\\'");
    		return newStr;
    }

	@Override
	public int compareTo(Object o) {
		if(!(o instanceof Item)) {
			throw new IFMLException("","");
		}
		Item other = (Item)o;
		String name = other.getName();
		int r = this.name.compareTo(name);
		
		return r;
	}
	
	public void sort() {
		Collections.sort(childItem);
		for (Item iF : this.childItem) {
			iF.sort();
		}
	}
}
